//
//  HJWithdrawalController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJWithdrawalController.h"
#import "HJWithdrawalView.h"

@interface HJWithdrawalController ()
@property(nonatomic,strong)UIScrollView * scrollView;
@property(nonatomic,strong)HJWithdrawalView *withdrawal;
@property(nonatomic,strong) NSNumber * selecte_num;

@end

@implementation HJWithdrawalController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configScroll];
    [self configBottom];
    [mNotificationCenter addObserver:self selector:@selector(allMoneyNotification:) name:@"allMoneyNotification" object:nil];
}

-(void)configScroll {
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, mScreenWidth, mScreenHeight-60)];
    self.scrollView.bounces = YES;
    self.scrollView.clipsToBounds = YES;
    self.scrollView.contentSize = CGSizeMake(mScreenWidth, 377);
    self.scrollView.backgroundColor = HJColorRGB(255);
    self.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.view addSubview:_scrollView];
    
    self.withdrawal = [[HJWithdrawalView alloc]init];
    self.withdrawal.frame= CGRectMake(0, 0, mScreenWidth, 327);
    self.withdrawal.money.text = self.money.length > 0 ? self.money : @"0.00";
    [self.scrollView addSubview:self.withdrawal];
}
-(void)allMoneyNotification:(NSNotification *)not {
    self.selecte_num = not.userInfo[@"num"];
}
-(void)configBottom {
    CGFloat H;
    if (mScreenHeight<812) {
        H = mScreenHeight -60 -64;
    }else {
        H = mScreenHeight-60-TabbarHeight-X_Bottom_Space;
    }
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, H, mScreenWidth, 60);
    [btn setTitle:@"确认提现" forState:UIControlStateNormal];
    [btn setTitleColor:HJColorRGB(0) forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [btn setBackgroundColor:HJColorRGB(255)];
    btn.layer.shadowOpacity = 0.2;
    btn.layer.shadowRadius = 3;
    btn.layer.shadowOffset = CGSizeZero;
    btn.layer.shadowColor = [UIColor blackColor].CGColor;
    [btn addTarget:self action:@selector(okWithdrawalBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [self.view bringSubviewToFront:btn];
}
-(void)okWithdrawalBtn {
    if (self.money.integerValue > 0 && self.withdrawal.allMoney.text.integerValue <=self.money.integerValue) {
        if (self.selecte_num.integerValue==0) {
            [self showHint:@"请选择提现方式"];
            return;
        }
        if (self.withdrawal.allMoney.text.doubleValue<=0) {
            [self showHint:@"请输入金额"];
            return;
        }
        if ([self.withdrawal.alipay_user_TF.text isEqualToString:@""]) {
            [self showHint:@"请输入支付宝账号"];
            return;
        }
        if ([self.withdrawal.username.text isEqualToString:@""]) {
            [self showHint:@"请输入支付宝认证实名"];
            return;
        }
        [self showTextHUDWithPleaseWait];
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"money"] = self.withdrawal.allMoney.text;
        params[@"true_name"] =self.withdrawal.username.text;
        params[@"mobile"] = self.withdrawal.alipay_user_TF.text;
        [TZHttpTool post:TransferAPI params:params success:^(NSDictionary *result) {
            [self hideTextHud];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        } failure:^(NSString *msg) {
            [self showErrorHUDWithError:msg];
        }];
    }else {
        [self showHint:@"没有足够的金额提现"];
        return;
    }
}
@end
